﻿using IoTSharp.Data.Taos;
using System.Threading;

namespace Test
{
    internal class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello, World!");

            try
            {
                Test1();
            }
            catch(Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }


            Console.ReadKey();
        }


        /// <summary>
        /// 单线程+多线程
        /// </summary>
        static void Test1()
        {
            TaosDbFactory.Init();

            List<TaosConnection> connections = new List<TaosConnection>();
            for (int i = 0; i < 15; i++)
            {
                int id = i + 1;
                try
                {

                    if (i >= 12)
                    {
                        Task.Factory.StartNew(() =>
                        {
                            Thread.Sleep(2 * 1000);
                            Console.WriteLine($"ID:{id},获取对象开始,：{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff")}");
                            try
                            {
                                var taos = TaosDbFactory.GetTaos();
                                if (taos == null)
                                {
                                    Console.WriteLine("获取对象失败为null");
                                }
                                else
                                {
                                    Console.WriteLine($"ID:{id},排队等待后获取到了新对象：{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff")}");
                                    connections.Add(taos);
                                }
                            }
                            catch (Exception ex)
                            {
                                Console.WriteLine(ex.ToString());
                            }

                            Console.WriteLine($"ID:{id},获取对象结束：{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff")}");

                        });
                    }
                    else
                    {
                        Console.WriteLine($"ID:{id},获取对象开始,：{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff")}");
                        var taos = TaosDbFactory.GetTaos();
                        Console.WriteLine($"ID:{id},获取对象结束：{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff")}");
                        if (taos == null)
                        {
                            Console.WriteLine("获取对象失败为null");
                        }
                        else
                        {
                            connections.Add(taos);
                        }
                    }

                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.ToString());
                    Console.WriteLine($"ID:{id},获取对象结束：{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff")}");
                }
                //Thread.Sleep(2 * 1000);
            }
            Console.WriteLine($"取出对象数量：{connections.Count}");
            Console.WriteLine($"归还对象");
            Thread.Sleep(5 * 1000);
            Console.WriteLine($"归还对象：{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff")}");
            foreach (var taos in connections)
            {
                TaosDbFactory.Return(taos);
            }
            Console.WriteLine($"归还对象结束：{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff")}");
            connections.Clear();
            Console.WriteLine($"取出对象数量：{connections.Count}");

            var taos2 = TaosDbFactory.GetTaos();
            if (taos2 != null)
            {
                Console.WriteLine($"取出新的对象");
            }
        }

        /// <summary>
        /// 多线程
        /// </summary>
        static void Test2()
        {
            TaosDbFactory.Init();

            List<TaosConnection> connections = new List<TaosConnection>();
            Task[] tasks = new Task[15];
            for (int i = 0; i < 15; i++)
            {
                int id = i + 1;
                tasks[i] = Task.Run(() =>
                {
                    Console.WriteLine($"ID:{id},获取对象开始,：{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff")}");
                    try
                    {
                        if (id >= 12)
                        {
                            Thread.Sleep(3* 1000);
                        }
                        var taos = TaosDbFactory.GetTaos();
                        if (taos == null)
                        {
                            Console.WriteLine("获取对象失败为null");
                        }
                        else
                        {
                            if (id >= 12)
                            {
                                Console.WriteLine($"ID:{id},排队等待后获取到了新对象：{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff")}");
                            }
                            else
                            {
                                Console.WriteLine($"ID:{id},获取对象结束,：{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff")}");
                            }

                            //if (i < 10)
                            //{
                                //Thread.Sleep(5 * 1000);
                                Console.WriteLine($"归还对象：{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff")}");
                                TaosDbFactory.Return(taos);
                                Console.WriteLine($"归还对象结束：{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff")}");
                            //}
                        }
                    }catch(Exception ex)
                    {
                        Console.WriteLine(ex.ToString());
                    }

                   // Console.WriteLine($"ID:{id},获取对象结束：{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff")}");
                });
                //Thread.Sleep(2 * 1000);
            }

            Task.WaitAll(tasks); // 等待全部线程完成
            var taos = TaosDbFactory.GetTaos();

        }
    }
}